﻿using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using ChinaSoft.BLL;
using ChinaSoft.Model;
using System.Collections.Generic;
using System;

namespace MidSoft.Sys
{
    public partial class AddUser : System.Web.UI.Page
    {
        protected System.Web.UI.HtmlControls.HtmlInputButton btnCancel;
        public string adminname = "管理部门";

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRoleData();
            }
        }

        private void BindRoleData()
        {
            Model_tb_MidSoft_Roles roles = new Model_tb_MidSoft_Roles();
            BLL_tb_MidSoft_Roles rolesMgr = new BLL_tb_MidSoft_Roles();
            ChinaSoft.Common.BindControl.BindDropDownList(ddlRole, rolesMgr.GetAllList(), "RoleName", "RolesId");
        }


        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {

        }
        #endregion

        protected void btnSave_Click(object sender, System.EventArgs e)
        {
            string strErr = "";
            if (this.txtUserAccount.Value.Trim().Length == 0)
            {
                strErr += "用户帐号不能为空！\\n";
            }
            if (this.txtUserPassword.Text.Trim().Length == 0)
            {
                strErr += "用户密码不能为空！\\n";
            }
            if (this.txtUserName.Text.Trim().Length == 0)
            {
                strErr += "用户名不能为空！\\n";
            }
            if (this.txtCompany.Text.Trim().Length == 0)
            {
                strErr += "单位名不能为空！\\n";
            }
            if (this.txtDepartMent.Text.Trim().Length == 0)
            {
                strErr += "部门不能为空！\\n";
            }
            if (!Maticsoft.Common.PageValidate.IsDateTime(txtBirthDay.Value))
            {
                strErr += "出生日期格式错误！\\n";
            }
            if (this.txtTel.Text.Trim().Length == 0)
            {
                strErr += "联系电话不能为空！\\n";
            }
            if (this.txtDuty.Text.Trim().Length == 0)
            {
                strErr += "Duty不能为空！\\n";
            }
            if (strErr != "")
            {
                Maticsoft.Common.MessageBox.Show(this, strErr);
                return;
            }
            string UserAccount = this.txtUserAccount.Value;
            string UserPassword = Maticsoft.Common.DEncrypt.DEncrypt.MD5(this.txtUserPassword.Text.Trim(), 16);
            string UserName = this.txtUserName.Text;

            string Company = this.txtCompany.Text;
            string DepartMent = this.txtDepartMent.Text;
            string Sex = this.rbtnMen.Checked ? "男" : "女";
            DateTime BirthDay = DateTime.Parse(this.txtBirthDay.Value);
            string Tel = this.txtTel.Text;
            string Duty = this.txtDuty.Text;
            DateTime CreateTime = DateTime.Now;

            int CreateUserId = 0;
            if (Session["currentUserID"] != null && Session["currentUserID"].ToString() != "")
                CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);

            int State = chboxKaiTong.Checked ? 0 : 1;
            #region 图片上传
            string UserImgFolder = "";
            string UserImgPath = "default.jpg";
            if (FileUpload1.HasFile)
            {
                string result = ChinaSoft.Common.UpFile.UploadFile(FileUpload1, Server.MapPath("/Uploads"), "", 5);
                if (result.Contains("错误"))
                {
                    Maticsoft.Common.MessageBox.Show(this, result);
                    return;
                }
                else
                {
                    UserImgFolder = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
                    UserImgPath = result;
                }
            }
            #endregion
            ChinaSoft.Model.Model_tb_MidSoft_Users model = new ChinaSoft.Model.Model_tb_MidSoft_Users();
            model.UserAccount = UserAccount;
            model.UserPassword = UserPassword;
            model.UserName = UserName;
            model.UserImgFolder = UserImgFolder;
            model.UserImgPath = UserImgPath;
            model.Company = Company;
            model.DepartMent = DepartMent;
            model.Sex = Sex;
            model.BirthDay = BirthDay;
            model.Tel = Tel;
            model.Duty = Duty;
            model.CreateTime = CreateTime;
            model.CreateUserId = CreateUserId;
            model.State = State;

            ChinaSoft.BLL.BLL_tb_MidSoft_Users bll = new ChinaSoft.BLL.BLL_tb_MidSoft_Users();
            int userid=bll.Add(model);
            #region 添加用户成功日志
            BLL_tb_MidSoft_log logMgr = new BLL_tb_MidSoft_log();
            Model_tb_MidSoft_log log = new Model_tb_MidSoft_log();
            log.CreateTime = DateTime.Now;
            log.description = "添加用户,ID=" + userid + ",UserAccount="+UserAccount;
            log.userAccount = "";
            if (Session["UserAccount"] != null && Session["UserAccount"].ToString() != "")
                log.userAccount = Session["UserAccount"].ToString();
            log.userName = "";
            if (Session["UserName"] != null && Session["UserName"].ToString() != "")
                log.userName = Session["UserName"].ToString();
            log.LogType = 1;
            int UserId=logMgr.Add(log);
            #endregion
            #region 添加用户选择的角色
            int RoleId = ChinaSoft.Common.Input.SetInt(ddlRole.SelectedValue);
            if (RoleId > 0)
            {
                Model_tb_MidSoft_UserHaveRoles modelUserHaveRoles = new Model_tb_MidSoft_UserHaveRoles();
                BLL_tb_MidSoft_UserHaveRoles bllUserHaveRoles = new BLL_tb_MidSoft_UserHaveRoles();
                modelUserHaveRoles.CreateTime = DateTime.Now;
                modelUserHaveRoles.RolesId = RoleId;
                modelUserHaveRoles.UsersId = UserId;
                bllUserHaveRoles.Add(modelUserHaveRoles);
            }
            #endregion
            Response.Write("<script>alert('成功！');top.window.parent.frames.item('workspace').location.reload();window.parent.$('#DivSysiframe').dialog(\"close\");</script>");
        }
    }
}